package com.ruoyi.hr.service.impl.welfare.welfareRegion;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.hr.domain.welfare.welfareregion.WelfareRound;
import com.ruoyi.hr.domain.welfare.welfareregion.WelfareType;
import com.ruoyi.hr.mapper.welfare.welfareRegion.WelfareRegionParameterConfigMapper;
import com.ruoyi.hr.mapper.welfare.welfareRegion.WelfareTypeMapper;
import com.ruoyi.hr.service.welfare.welfareRegion.WelfareRegionParameterConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class WelfareRegionParameterConfigServiceImpl implements WelfareRegionParameterConfigService {

    @Autowired
    WelfareRegionParameterConfigMapper welfareRegionParameterConfigMapper;

    @Autowired
    private WelfareTypeMapper welfareTypeMapper;

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<Map<String, Object>> findData() {
        //先取出所有类型
        List<WelfareType> welfareTypes = welfareTypeMapper.findAll();
        //拼接sql
        StringBuffer sql = new StringBuffer("select *");
        int num = 0;
        //查询进位方式
        sql.append(" from (select 薪资地区 地区名称,max(CityId) 序号,max(地区代码) 地区代码,");
        for (WelfareType welfareType : welfareTypes) {
            String typeReplace =  welfareType.getTitle().replaceAll(" ", "").replaceAll("-", "").replaceAll("–", "");
            sql.append("sum(case 薪资类型 when N'");
            sql.append(welfareType.getTitle());
            sql.append("' then 基数上限 else null end)");
            sql.append(welfareType.getCode() + "@@基础上限,");

            sql.append("sum(case 薪资类型 when N'");
            sql.append(welfareType.getTitle());
            sql.append("' then 基数下限 else null end)");
            sql.append(welfareType.getCode() + "@@基础下限,");

            sql.append("sum(case 薪资类型 when N'");
            sql.append(welfareType.getTitle());
            sql.append("' then 比例 else null end)");
            sql.append(welfareType.getCode() + "@@比例,");

            sql.append("sum(case 薪资类型 when N'");
            sql.append(welfareType.getTitle());
            sql.append("' then 加项 else null end)");
            sql.append(welfareType.getCode() + "@@加项,");

            sql.append("sum(case 薪资类型 when N'");
            sql.append(welfareType.getTitle());
            sql.append("' then RoundId else null end)");
            sql.append(welfareType.getCode() + "@@进位方式id");
            num++;
            if (num <= welfareTypes.size() - 1) {
                sql.append(", ");
            }
        }
        sql.append(" from zVw_welfareRegionParameterConfig group by 薪资地区) a order by 序号");
        Map<String, Object> map = new HashMap<>();
        map.put("sql", sql);
        return welfareRegionParameterConfigMapper.findData(map);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int update(String cityId, String benefitTypeAndXl, String updVal) {
        String[] arry = benefitTypeAndXl.split("@@");
        String benefitType = arry[0];
        String xl = arry[1];
        xl = getField(xl);
        //先查数据库有无这个类型
        if(welfareRegionParameterConfigMapper.findCountCityValByBenefitType(cityId,benefitType) == 0){
            welfareRegionParameterConfigMapper.insertCityValByBenefitType(cityId,benefitType);
        }
        return welfareRegionParameterConfigMapper.update(cityId,benefitType,xl,updVal);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int updateRound(String cityId, String benefitType, String xl, String updVal) {
        return welfareRegionParameterConfigMapper.updateRound(cityId,benefitType,xl,updVal);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public List<WelfareRound> findWelfareRound() {
        return welfareRegionParameterConfigMapper.findWelfareRound();
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int findCountCityValByBenefitType(String cityId, String benefitType) {
        return welfareRegionParameterConfigMapper.findCountCityValByBenefitType(cityId,benefitType);
    }

    @DataSource(value = DataSourceType.SLAVE)
    @Override
    public int insertCityValByBenefitType(String cityId, String benefitType) {
        return welfareRegionParameterConfigMapper.insertCityValByBenefitType(cityId,benefitType);
    }

    /**
     * 根据获取的汉字获取是什么项目
     *
     * @param val
     * @return
     */
    public String getField(String val) {
        if ("基础上限".equals(val)) {
            return "MaxBase";
        } else if ("基础下限".equals(val)) {
            return "MinBase";
        } else if ("基础下限".equals(val)) {
            return "MinBase";
        } else if ("比例".equals(val)) {
            return "Rate";
        } else if ("加项".equals(val)) {
            return "Fixed";
        } else {
            return "RoundID";
        }
    }
}
